Tutorial Contents

Detect rhythm episodes

Algorithm

Waveform rhythms

Post filter

Pre filter

Spectral analysis

Event rhythms

Post filter

List rhythmicity/period

Contents

Detect Rhythm Episodes

DataView contains facilities to help detect and mark periods of rhythmic or oscillatory activity embedded in aperiodic data. The input to these facilities is either the raw waveform data themselves, or events that have been extracted from the data.

Algorithm

The key defining feature that makes something rhythmic is the regularity of occurrence of particular activity motifs. If these repeat at more-or-less fixed intervals, that activity is rhythmic. If the motifs occurs at random intervals, the activity is arrhythmic. This is, of course, not a binary distinction, and exactly how regular the activity has to be before it is considered rhythmic is a matter of judgement.

For raw waveforms, regularity is detected by looking for a trough followed by a peak in the autocorrelation of the waveform.  For events, regularity is detected by looking for a contiguous series of events with a low coefficient of variation in the intervals between them.

Detecting Rhythms in Waveforms

Note that this algorithm works best if you already know that your data contains some rhythmic activity through visual inspection, and you want an objective way of detecting all such activity within the recording.

This shows a segment of an audio recording of fly courtship song. There are silent periods, and then periods where the fly emits a buzzing sound. The display shows a transition from non-buzzing to buzzing. We want to detect the buzzes. (Note: the file is actually a montage from which some data have been eliminated.)

Quick Start Steps

Trace auto-correlation rhythm detect dialog
Detecting rhythms in waveforms. The Trace auto-correlation rhythm detect dialog.

Details

The analysis works by taking a chunk of data of length set by the Sliding window parameter, and calculating the autocorrelation coefficients at lags up to the defined Maximum period. It then searches forward through the coefficients to find the first trough followed by a peak in the coefficient values. If the trough-to-peak height is above the Threshold value, then the window is considered to contain rhythmic activity. It then advances to the next chunk by the defined Step and repeats the analysis. The step would normally be shorter than the sliding window, so that analysis regions overlap. Rhythmic regions are marked by events in the selected channel.

Note: autocorrelation coefficients are insensitive to the amplitude of the source data within the analysis window, so the Threshold refers to the degree of rhythmicity, not the size of the raw signal (see Post filter below if that is a matter that needs consideration) .

The two graphs in the dialog show a preview of the analysis. The upper graph shows a Sliding window length of the raw data that is being analysed, the lower graph shows the correlation coefficients for that window.

There are 4 key parameters for rhythm detection.

The key parameters can be adjusted using the interactive preview displayed in the two graphs of the dialog box. The location of the preview data within the main recording can be set by editing the preview start time explicitly, or by using the navigation buttons above the graph. A quick way to get the preview in the right location is to centre a section of rhythmic activity in the main view, and then click the Sync button, which makes the data preview start at the main screen centre. The sample data file is already correctly positioned, so you can click Sync immediately.

This moves the preview one sliding window earlier in the file, and we can see that the data display is now half occupied by non-rhythmic data, and there is a corresponding reduction in the Rhythmicity index.

The coefficient preview still shows peaks and troughs, but these are rather chaotic, and much smaller. The preview window shows that data in this region do have some rhythmicity (probably microphonics), but it is lower amplitude and higher frequency than the main song rhythm. The rhythmicity index is well below the default Threshold value, so it would not be scored as rhythmic in analysis.

The first 3 key parameters can be doubled and halved simultaneously using the Compress (><) and Expand (<>) buttons respectively.  If the lag value is edited directly, then the Sliding window and Step parameters change automatically to be 3 times and 1 times the lag value respectively. This is because this is a reasonable default setting. However, the Sliding window and Step can be set independently of the lag if desired.

Note that because autocorrelation essentially works in the frequency domain, the timing precision with which the onset and offset of rhythms is detected is limited by the size of the Sliding window. Longer windows allow the detection of weaker rhythms contaminated by noise, but at the expense of loss of precision in timing. This loss can be partly offset by setting the Step value so that the Sliding windows overlap, as is the case with the default settings.

Event channel a shows the output of the analysis. The main episodes of fly song have clearly been detected, but there are also quite a few brief events which appear to delimit signals within the noise (e.g. the first few events). However, detailed examination of these shows that there is indeed apparent rhythmic activity in the data, but this is brief and low amplitude. The autocorrelation algorithm is amplitude insensitive, and so these episodes are detected along with the lengthier and higher-amplitude episodes which are the ones we are interested in. We can improve things by applying a post-filter to the events.

Post-Filter

Now only events in which the data trace peak-to-peak amplitude exceeds 25 are retained. These are shown in event channel b (at the bottom of the view in the figure below).

Rhythms in fly courtship song
Rhythmic buzzes in a fly courtship song. Event channel a (at the top of the view) shows all detected periods of rhythmic activity. Event channel b (at the bottom of the view) shows periods of rhythmic activity where the amplitude exceeds a set threshold.

Pre-Filter: extracellular recording

The Trace auto-correlation rhythm detect facility contains pre-filtering options that allow you to rectify and smooth the signal before analysing it, which would be appropriate for an extracellular recording. However, the Transform: De-mean/median, rectify, smooth menu command give more flexibility, and is recommended as an alternative pre-processing step.

Spectral analysis

The numerical value associated with each event detected by this method shows the average period (ms) of the rhythm during the event. The second buzz episode has a slightly longer period than the first (7.25 vs 6.13 ms) as well as having a lower amplitude.

You should hear that the second buzz is slightly lower in pitch than the first, as well as being softer. You should also hear that there is variation in the pitch (frequency) within the buzz episodes, so be aware that the event numerical value is just a guide to the average value of the period.

You can examine the frequency characteristics of the buzz episodes in more detail with spectral analysis. Full details of the various options are given here, but the following instructions "cut to the chase".

a
Spectrogram of fly courtship buzz episodes
b
Spectrum of fly courtship buzz
Spectral analysis of buzzes in fly courtship song. a. Spectrogram showing several buzz episodes. b. Spectrum of the buzz episode section with the highest power. The text at the top shows the power associated with each frequency bin. Bin 21 (164.06 Hz) has the highest power.

Detecting Rhythms in Events

DataView can also look for rhythms within event trains without reference to the waveform data. Bouts of rhythmic activity are identified as regions in which the interval between events has a low coefficient of variationThe coefficient of variation is simply the standard deviation divided by the mean. (CV), i.e. the interval does not vary much.

The dialog preview should now look like this:

Threshold detect event rhythms
Events detected from a fly courtship buzz by zero-crossing threshold.

The events within the buzz section of data are regularly spaced, reflecting the regular oscillations of the waveform. The events within the noise data are much less regular.

The dialog box should now look like the image below.

Detect rhythms in events
Detect rhythms in events.

The Minimum interval count specifies the minimum number of consecutive regularly-spaced intervals that will be counted as a rhythm. The default value is 20, but if you know that the episodes of rhythmic activity that you are looking have fewer cycles than this, you can reduce this value. The minimum count is just 2 (i.e. three events with just 2 intervals between them). However, be aware that the chance of detecting spurious rhythms in random data is very high when a “rhythm” is defined with such a short episode length.

The algorithm starts at the beginning of the specified analysis region (which by default is the visible screen) and takes a series of consecutive intervals between events, with the number set by the Minimum interval count parameter. It calculates the mean and standard deviation of the intervals in this series, and hence the coefficient of variation. It then advances by one event, and repeats the calculation. This continues through the specified analysis region. Data are regarded as rhythmic when the CV drops below a threshold set as the maximum coefficient of variation (Max coeff var: there is a draggable horizontal cursor in the CV graph which interacts with this value).

There are two graphs in the dialog box, both initially displaying data derived from the section of data visible in the main display. The upper graph shows the raw intervals between events (red dots) and the running mean of the intervals over the Minimum interval count series (blue line). The lower graph shows the CV of the event series starting at the time given by the X axis. It is clear that as the data make the transition from baseline noise to the buzz, the intervals become regular and the CV drops to a low value.

The reason that the CV and running mean lines stop before the right-hand end of the graph is that their time values are registered to the start of the interval series, which by default is 20 intervals long. So their values terminate 20 intervals before the interval values themselves.

Now two episodes of rhythm are detected (i.e. the CV graph drops below threshold twice).

Examination of the data in the main view shows that the first episode is in fact generated by a period in which the random noise in the signal just happens to produce a series of events with fairly regular intervals. This illustrates an important point about the algorithm: it is scale invariant. The spurious rhythm has much higher frequency than the real rhythm, but is still detected due to the definition of a rhythm as a period of events with regular intervals between them. Similarly, since the algorithm is working with events, it has no knowledge of the amplitude of the signal generating the events. There are many filtering options within DataView that could eliminate the spurious rhythm, but the easiest is to return to a longer sliding window.

The main view should now look like this:

Rhythms detected by events
Rhythmic episodes of the fly courtship song detected by events.

Post filter

There are two "issues" with the events in channel b: false negative and false positive episode detection.

False negative

The first issue is evident in the occurrence of occasional "drop-outs", where what looks like a single episode of buzzing has been detected as two events (e.g. events 9 and 10). If you zoom in on these using the Horizontal magnify button (magnify timebase) in the main toolbar you will see that they mainly caused by "jitter" in the initial event-detection routine where the waveform crosses the 0 level more than once within a cycle. This briefly elevates the CV and disrupts the event rhythm. These can be dealt with as follows:

Now the search algorithm makes a second pass through the data and merges any pair of episodes that are separated by a gap that is less than a specified multiple of the mean of the interval of the first episode. With the default multiplier of 3, events 4+5 and 9+10 in channel b are merged, but there is still a gap between events 2 and 3 because the period of jitter is more extended.

Now each buzz episode is marked by a single event, which was the desired outcome.

Note: An alternative approach would be to apply a moving average smoothing filter to the raw waveform before using the event Threshold detection method, thus reducing the jitter at source, . This can reduce episode disruption caused by jitter, but can increase the second of the two issues mentioned above - false positive detection.

False positive

The second issue is evident in event 6 in channel b (event 4 in the new channel c). Here, a section of noise happens to just meet the criteria for rhythmicity, and has been marked as such. The algorithm is behaving correctly, but the low amplitude of the signal encompassed by the event suggests that it is not a real buzz episode.

Since the problem involves the actual data rather than the event timing, we have to use facilities outside the Rhythm detect dialog, since this only deals with events.

We can deal with this as follows:

You now have a series of events in channel d marking each buzz episode within the file.

List Rhythmicity/Period

You can get a list of the rhythmicity index and average period for each buzz episode.

Note: This is independent of any of the preceding analyses - you can use the following procedure even if you simply marked the buzz episodes manually.

The Results dialog will display a list of the Event Id, on-time, rhythmicity index, and the average rhythm period for each event. The values are calculated "behind the scenes" from a statistical autocorrelation (normalized autocovariance) analysis of the waveforms within each event.

You could measure these values explicitly for a single episode thus:

The rhythmicity index is the trough-to-peak amplitude from the first trough in the covariance graph (marked with a red tick) to the second peak (marked with a blue tick), and the period is the lag to the second peak. The index (idx) and period are reported in the Rhythmicity section of the Correlation group of the dialog.